package com.oa.log.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.data.jpa.domain.Specification;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.oa.log.entity.LogEntity;

public class LogEntityDto {
	private Long id;
	private String fullname;
	private Long userId;
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	private Date oprateTime;
	private String oprate;
	private String ip;
	private boolean success;

	public Long getId() {
		return id;
	}

	public String getFullname() {
		return fullname;
	}

	public Long getUserId() {
		return userId;
	}

	public Date getOprateTime() {
		return oprateTime;
	}

	public String getOprate() {
		return oprate;
	}

	public String getIp() {
		return ip;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public void setOprateTime(Date oprateTime) {
		this.oprateTime = oprateTime;
	}

	public void setOprate(String oprate) {
		this.oprate = oprate;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	@Override
	public String toString() {
		return "LogEntityDto [id=" + id + ", fullname=" + fullname + ", userId=" + userId + ", oprateTime=" + oprateTime
				+ ", oprate=" + oprate + ", ip=" + ip + ", success=" + success + "]";
	}

	public static LogEntityDto toLogEntityDto(LogEntity logEntity, String fullname) {
		LogEntityDto logEntityDto = new LogEntityDto();
		BeanUtils.copyProperties(logEntity, logEntityDto);
		logEntityDto.setFullname(fullname);
		return logEntityDto;
	}

	public static Specification<LogEntity> getLogManagerQuery(LogEntityDto logEntityDto) {
		return new Specification<LogEntity>() {
			@Override
			public Predicate toPredicate(Root<LogEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (logEntityDto.getUserId() != null) {
					Predicate predicate = cb.equal(root.get("userId").as(Long.class), logEntityDto.getUserId());
					list.add(predicate);
				}
				if (logEntityDto.getOprate() != null && logEntityDto.getOprate().trim().length() > 0) {
					Predicate predicate = cb.like(root.get("oprate").as(String.class),
							"%" + logEntityDto.getOprate() + "%");
					list.add(predicate);
				}
				if (logEntityDto.getIp() != null && logEntityDto.getIp().trim().length() > 0) {
					Predicate predicate = cb.like(root.get("ip").as(String.class), "%" + logEntityDto.getIp() + "%");
					list.add(predicate);
				}

				Predicate[] predicates = new Predicate[list.size()];
				return cb.and(list.toArray(predicates));
			}
		};
	}

}
